package cutCriteria;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import neural.Individual;

import core.Problem;

public class OptimumEnvironmentCutCriteria implements CutCriteria {

	private int counter;
	private Double optimum;
	private int entorno;
	private Integer iterations;

	public OptimumEnvironmentCutCriteria(Double optimum, int cota,
			Integer iterations) {
		this.optimum = 0.0001;
		this.entorno = cota;
		this.iterations = iterations;
		counter = 0;

	}

	@Override
	public Boolean shouldFinish(Problem problem) {
		if (optimum == null) {
			return false;
		}
		if (getMax(problem.getIndividuals() )<= entorno) {
			return false;
		} else {
			return true;
		}


	}

	private double getMax(List<Individual> individuals) {
		List<Double> aptitudes = new ArrayList<Double>();

		for (Individual d : individuals) {
			aptitudes.add(d.getFitness());
		}

		Collections.sort(aptitudes, new Comparator<Double>() {

			@Override
			public int compare(Double o1, Double o2) {

				return (int) Math.ceil(o2 - o1);
			}

		});

		return aptitudes.get(0);

	}
}
